package com.agileboot.attendance.domain.order.model;

import com.agileboot.attendance.domain.order.db.AttOrderPersonnelService;
import com.agileboot.attendance.domain.order.entity.AttOrderPersonnelEntity;
import com.agileboot.attendance.domain.personnel.db.AttPersonnelService;
import com.agileboot.common.exception.ApiException;
import com.agileboot.common.exception.error.ErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 订单人员关联模型工厂
 *
 * @author agileboot
 */
@Component
@RequiredArgsConstructor
public class OrderPersonnelModelFactory {

    private final AttOrderPersonnelService orderPersonnelService;
    private final AttPersonnelService personnelService;

    /**
     * 创建空模型
     *
     * @return 模型对象
     */
    public OrderPersonnelModel create() {
        return new OrderPersonnelModel();
    }

    /**
     * 根据ID加载模型
     *
     * @param id 关联ID
     * @return 模型对象
     */
    public OrderPersonnelModel loadById(Long id) {
        AttOrderPersonnelEntity entity = orderPersonnelService.getById(id);
        if (entity == null) {
            throw new ApiException(ErrorCode.Business.COMMON_OBJECT_NOT_FOUND, id, "订单人员关联");
        }
        
        OrderPersonnelModel model = OrderPersonnelModel.fromEntity(entity);
        model.setPersonnelEntity(personnelService.getById(entity.getPersonnelId()));
        
        return model;
    }
} 